package com.gxa.hotel.portal.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonObject;
import com.gxa.hotel.core.pojo.domain.Hotel;
import com.gxa.hotel.core.util.RestUtils;
import com.gxa.hotel.portal.service.HotelCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class HotelCacheServiceImpl implements HotelCacheService {

    private static final String HOTEL="hotel:";

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public String selectDetail() {

        BoundValueOperations stringOperations = redisTemplate.boundValueOps(HOTEL + "detail");
       // stringOperations.expire(10,TimeUnit.MINUTES);

        Object o = stringOperations.get();
        if(StringUtils.isEmpty(o)){
            String url="http://localhost:8080/hotel/detail";
            MultiValueMap<String,Object> params= new LinkedMultiValueMap<>();
            //params.add("id",id);
            Map map = RestUtils.postWithParams(url,params);
            o=  map.get("data");
            stringOperations.set(o,10,TimeUnit.MINUTES);
        }
        return o.toString();
    }
}
